package com.yst.gateway.monitor.controllers;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yst.gateway.monitor.common.beans.Response;
import com.yst.gateway.monitor.services.QzDownloadService;


@RestController
@RequestMapping(value = "/qz/download")
public class QzAttachmentController {
	
	@Autowired
	private QzDownloadService qzDownloadService;

	@RequestMapping(value = "/filedown", method = RequestMethod.GET)
	public void download(String filename, HttpServletRequest request,HttpServletResponse response) {
		
		String path = request.getSession().getServletContext().getRealPath("/");
		if(path != null){
			path = path.replaceAll("\\\\", "/");
		}
		if(path.endsWith("/")){
			path = path+"manage/upload/";
		}else{
			path = path+"/manage/upload/";
		}
		
		File file = null;
		
		BufferedInputStream bis = null;  
	    BufferedOutputStream bos = null;  
	 
	    response.setContentType("application/octet-stream");  
	    try {
	    	file = new File(path+filename);
	    	if(file.exists()){
	    		if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {  
	    		    filename = URLEncoder.encode(filename, "UTF-8");  
	    		} else {  
	    		    filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");  
	    		} 
	    		response.setHeader("Content-disposition", "attachment; filename="  
	    			    + filename);
	    			 
	    		    response.setHeader("Content-Length", String.valueOf(file.length()));  
	    		    
	    		    bis = new BufferedInputStream(new FileInputStream(file));  
	    		    
	    		    bos = new BufferedOutputStream(response.getOutputStream());  
	    		    byte[] buff = new byte[1024];  
	    		    int bytesRead;  
	    		    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
	    		      bos.write(buff, 0, bytesRead);  
	    		      bos.flush();
	    		    }  
	    		    bos.flush();
	    		    bis.close();
	    		    bos.close(); 
	    	}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();			
		}		
	}
	
	@RequestMapping(value = "/filedown2", method = RequestMethod.GET)
	public Response download2(String filename, HttpServletRequest request,HttpServletResponse response) {
		Response res = new Response();
		
		String path = request.getSession().getServletContext().getRealPath("/");
		if(path != null){
			path = path.replaceAll("\\\\", "/");
		}
		if(path.endsWith("/")){
			path = path+"manage/upload/";
		}else{
			path = path+"/manage/upload/";
		}
		
		File file = null;
		
		BufferedInputStream bis = null;  
	    BufferedOutputStream bos = null;  
	 
	    response.setContentType("application/octet-stream");  
	    try {
	    	file = new File(path+filename);
	    	if(file!=null){
	    		response.setHeader("Content-disposition", "attachment; filename="  
	    			    + new String(filename.getBytes("utf-8"), "ISO8859-1"));
	    			 
	    		    response.setHeader("Content-Length", String.valueOf(file.length()));  
	    		    
	    		    bis = new BufferedInputStream(new FileInputStream(file));  
	    		    
	    		    bos = new BufferedOutputStream(response.getOutputStream());  
	    		    byte[] buff = new byte[1024];  
	    		    int bytesRead;  
	    		    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
	    		      bos.write(buff, 0, bytesRead);  
	    		      bos.flush();
	    		    }  
	    		    bos.flush();
	    		    bis.close();
	    		    bos.close();	    		    
	    	}
	    	
			res.success(bos);
		    
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		return res;
	}
}
